package info.itloser.learnkotlindemo.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import info.itloser.learnkotlindemo.R
import info.itloser.learnkotlindemo.adapter.BaseItemAdapter
import info.itloser.learnkotlindemo.databinding.FragmentExampleBinding

class ExampleFragment : Fragment() {

    private lateinit var exampleViewModel: ExampleViewModel

    private lateinit var binding: FragmentExampleBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        setHasOptionsMenu(true)

        exampleViewModel = ViewModelProvider(this).get(ExampleViewModel::class.java)
        binding = FragmentExampleBinding.inflate(layoutInflater, container, false)

        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        //初始化数据
        exampleViewModel.initBaseItems(requireContext())

        binding.rvExampleBase.layoutManager = LinearLayoutManager(requireContext())
        binding.rvExampleBase.adapter =
            BaseItemAdapter(exampleViewModel.baseItems, requireContext())


    }
}